/**
 * 
 */
package cn.mxz.robots.province;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 云南
 * 排名:完成
 * 去娘排名：完成
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-22
 */
public class Yunnan extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("222453072", "661236", "130825199708", "234", "");
		Yunnan gansu = new Yunnan(student);
		System.out.println(gansu.login(""));
	}

	public Yunnan(Student student)
	{
		super(student);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://www.ynzs.cn/gk2015/index.php", mSha1);
		loginRequest.setCharSet("utf-8", "gb2312");
		loginRequest.addHeader("Referer", "http://www.ynzs.cn/gk2015/");
		loginRequest.addParams("txtXH", mStudent.getStudentId());
		loginRequest.addParams("txtCXMM", mStudent.getStudentPwd());
		loginRequest.addParams("Submit.x", "31");
		loginRequest.addParams("Submit.y", "22");
		loginRequest.addParams("Submit", "%CC%E1%BD%BB");
		// System.out.println(loginRequest.getParams());
		String loginHtml = curlPost(loginRequest).replace("&nbsp", "");
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("PartLmTit"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("未找到查询结果") || loginHtml.contains("未找到查询结果"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("考生号") || loginHtml.contains("登录号"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else if (loginHtml.contains("验证码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println(loginHtml);
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();

		Elements trs = loginDocument.select("table.PartTable2").get(0).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trs.get(1).select("td").get(1).text().trim());
		result.put("ranking", regexMatch(trs.get(7).select("td").get(0).text().trim(), "(\\d+)"));
		result.put("province", "云南省");

		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trs.get(1).select("td").get(3).text().trim());
		scoreObject.put("数学", trs.get(2).select("td").get(1).text().trim());
		scoreObject.put("外语", trs.get(3).select("td").get(1).text().trim());
		scoreObject.put("综合", trs.get(4).select("td").get(1).text().trim());
		scoreObject.put("总分", trs.get(6).select("td").get(1).text().trim());
		scoreObject.put("量化成绩", trs.get(5).select("td").get(1).text().trim());

		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(30);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2014/107070.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/107069.html"));
		// 文科 http://www.gxeduw.com/news/2014/107069.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");
		Integer pre = 0;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = as.get(i).select("td").get(0).text();
			String countString = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			String leiji = regexMatch(as.get(i).select("td").get(2).text(), "(\\d+)");

			Integer count = Integer.parseInt(countString);
			if (countString.equals(""))
				continue;
			Integer ranking = pre + 1;
			pre = Integer.parseInt(leiji);

			if (scoreString.contains("—"))
			{
				//System.out.println(scoreString);
				Integer score = null;
				String beginString = regexMatch(scoreString, "— (\\d+)");
				String endString = regexMatch(scoreString, "(\\d+) —");
				Integer bs = Integer.parseInt(beginString);
				Integer es = Integer.parseInt(endString);
				Integer inc = count / 5;
				for (int j = es; j > bs - 1; j--)
				{
					score = j;
					if (j != es)
					{
						ranking += inc;
					}
					count = inc;
					GaoKaoRank rank = new GaoKaoRank();

					rank.setScore(score);
					rank.setCount(count);
					rank.setType(BEN_KE_WEN_KE);
					rank.setGaoKaoProvince(province);
					rank.setRanking(ranking);

					rankService.save(rank);
					System.out.println(rank);
					System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
				}
			}
			else
			{
				Integer score = Integer.parseInt(regexMatch(scoreString, "(\\d+)"));
				GaoKaoRank rank = new GaoKaoRank();

				rank.setScore(score);
				rank.setCount(count);
				rank.setType(BEN_KE_WEN_KE);
				rank.setGaoKaoProvince(province);
				rank.setRanking(ranking);

				rankService.save(rank);
				System.out.println("score: " + scoreString + "count: " + countString + "rank: " + ranking);
				System.out.println(rank);
			}
		}
	}
}
